package com.example.administrator.mybottomnavigationbar;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;

import java.util.ArrayList;
import java.util.List;

public class MainActivity2 extends FragmentActivity {
    private BottomNavigationBar mBottomNavigationBar;
    private List<Fragment> fragments = new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main2);
        initBottomNavigationBar();

    }

    private void initBottomNavigationBar() {
        mBottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        String[] mTitles = new String[]{"首页", "发现", "信息", "我的"};
        mBottomNavigationBar
                .setActiveColor(R.color.black)
                .setInActiveColor(R.color.gray)
                .setBarBackgroundColor(R.color.white);

        mBottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);
        mBottomNavigationBar
                .setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);


        mBottomNavigationBar
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher_round, mTitles[0])
                        .setInactiveIcon(ContextCompat.getDrawable(this, R.mipmap.ic_launcher)))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher_round, mTitles[1])
                        .setInactiveIcon(ContextCompat.getDrawable(this, R.mipmap.ic_launcher)))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher_round, mTitles[2])
                        .setInactiveIcon(ContextCompat.getDrawable(this, R.mipmap.ic_launcher)))
                .addItem(new BottomNavigationItem(R.mipmap.ic_launcher_round, mTitles[3])
                        .setInactiveIcon(ContextCompat.getDrawable(this, R.mipmap.ic_launcher)))
                .setFirstSelectedPosition(0)
                .initialise();//所有的设置需在调用该方法前完成```

        mBottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {//这里也可以使用SimpleOnTabSelectedListener
            @Override
            public void onTabSelected(int position) {//未选中 -> 选中
                switchTab(position);
            }

            @Override
            public void onTabUnselected(int position) {//选中 -> 未选中


            }

            @Override
            public void onTabReselected(int position) {//选中 -> 选中

            }
        });
    }

    int curFragment = -1;

    private void switchTab(int position) {
        FragmentManager manager = getSupportFragmentManager();
        Fragment fragment = manager.findFragmentByTag("" + position);
        FragmentTransaction beginTransaction = manager.beginTransaction();
        if (fragment == null) {
            if (manager.findFragmentByTag("" + curFragment) != null) {
                beginTransaction.hide(fragments.get(curFragment));
            }
            beginTransaction.add(R.id.fram_layout, fragments.get(position), "" + position)
                    .show(fragments.get(position))
                    .commit();


        } else if (curFragment != position) {
            beginTransaction.hide(fragments.get(curFragment))
                    .show(fragments.get(position))
                    .commit();
        }
        curFragment = position;
    }
}
